
package CCT220_Aula14_Exercicio01_13106;


import javax.swing.JOptionPane;


/**
 * </br>******************************
 * </br>Project: CCT220
 * </br>Package: CCT220_Aula14_Exercicio01_13106
 * </br>Class: Exercicio01Excecao
 * </br>Last Change: 05/10/2011 09:15:32
 * </br>Description: A simple exception exercise
 * </br>******************************</br>
 * 
 * @author marcel
 * @author 13106
 * 
 */
public class Exercicio01Excecao {

	private final int resultado;


	/**
	 * </br>******************************
	 * </br>Constructor for
	 * </br>Package: CCT220_Aula14_Exercicio01_13106
	 * </br>Class: Exercicio01Excecao
	 * </br>LastChange: 05/10/2011 09:15:39
	 * </br>******************************</br>
	 * 
	 */
	public Exercicio01Excecao() {

		String num = "";
		String den = "";
		do {
			do {
				num = (JOptionPane
				        .showInputDialog(
				                null,
				                "Entre com o numerador ou digite sair para fechar o programa",
				                "Entrada de dados",
				                JOptionPane.INFORMATION_MESSAGE));
				if (num == null) {
					System.exit(0);
				}
			} while (!isNumber(num));
			do {
				den = (JOptionPane
				        .showInputDialog(
				                null,
				                "Entre com o denominador ou digite sair para fechar o programa",
				                "Entrada de dados",
				                JOptionPane.INFORMATION_MESSAGE));
				if (den == null) {
					System.exit(0);
				}
			} while (!isNumber(den));

		} while (!canDivide(num, den));
		resultado = divide(Integer.parseInt(num), Integer.parseInt(den));
		JOptionPane.showMessageDialog(null, "O resultado de " + num + " / "
		        + den + " = " + resultado, "Resultao",
		        JOptionPane.INFORMATION_MESSAGE);

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Aula14_Exercicio01_13106
	 * </br>Class: Exercicio01Excecao
	 * </br>Method: isNumber
	 * </br>Last Change: 05/10/2011 09:22:02
	 * </br>Description: Verify if num is a number or close the program
	 * </br>******************************</br>
	 * 
	 * @param num
	 * @return true | false
	 */
	private boolean isNumber(String num) {

		if (num.equalsIgnoreCase("sair")) {
			System.exit(0);
		}
		try {
			Integer.parseInt(num);
		} catch (NumberFormatException exNumberFormat) {
			JOptionPane.showMessageDialog(null, num + " deve ser um numero!",
			        "Erro de entrada", JOptionPane.ERROR_MESSAGE);
			return false;
		}

		return true;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Aula14_Exercicio01_13106
	 * </br>Class: Exercicio01Excecao
	 * </br>Method: canDivide
	 * </br>Last Change: 05/10/2011 09:15:46
	 * </br>Description: Verify division operation
	 * </br>******************************</br>
	 * 
	 * @param a
	 * @param b
	 * @return true | false
	 */
	private boolean canDivide(String a, String b) {

		int intA;
		int intB;
		try {
			intA = Integer.parseInt(a);
			intB = Integer.parseInt(b);
		} catch (NumberFormatException exNumberFormat) {
			JOptionPane.showMessageDialog(null, "\nErro Messge:\n"
			        + exNumberFormat.getMessage(), "Erro",
			        JOptionPane.ERROR_MESSAGE);
			return false;
		}
		try {
			divide(intA, intB);
		} catch (ArithmeticException exArithimeticException) {
			JOptionPane.showMessageDialog(null, "\nErro Messge:\n"
			        + exArithimeticException.getMessage(), "Erro",
			        JOptionPane.ERROR_MESSAGE);
			return false;
		}
		return true;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Aula14_Exercicio01_13106
	 * </br>Class: Exercicio01Excecao
	 * </br>Method: divide
	 * </br>Last Change: 05/10/2011 09:16:12
	 * </br>Description: Divide an integer by another and return your value. In
	 * case of inconsistency it throws an ArithimeticException
	 * </br>******************************</br>
	 * 
	 * @param a
	 * @param b
	 * @return a/b
	 * @throws ArithmeticException
	 */
	private int divide(int a, int b) throws ArithmeticException {

		return a / b;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Aula14_Exercicio01_13106
	 * </br>Class: Exercicio01Excecao
	 * </br>Method: main
	 * </br>Last Change: 05/10/2011 09:17:35
	 * </br>Description: Call the constructor for Exercicio01Excecao
	 * </br>******************************</br>
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		new Exercicio01Excecao();
	}
}
